#include "XmlFunc.h"

QString XMLFunc::ConfigXmlPath;
bool XMLFunc::ConfigXmlFileInit(const QString &filePath)
{
    QFile xmlFile(filePath);
    if(!xmlFile.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        ELOG(QString("文件打开失败: " + filePath));
        xmlFile.close();
        return false;
    }

    // 创建 QXmlStreamWriter 对象并设置输出设备为文件
    QXmlStreamWriter xmlWriter(&xmlFile);
    xmlWriter.setAutoFormatting(true);  // 设置自动格式化，使 XML 文件更易读
    xmlWriter.writeStartDocument();     // 开始文档

    // 写入根元素
    xmlWriter.writeStartElement("root");

    // 写入Language元素
    xmlWriter.writeStartElement("Language");
    xmlWriter.writeCharacters("Chinese");       // 添加文本内容
    xmlWriter.writeEndElement();

    // 写入ShowMode元素
    xmlWriter.writeStartElement("ShowMode");
    xmlWriter.writeCharacters("DAY");
    xmlWriter.writeEndElement();

    // 写入VideoImagePath元素
    xmlWriter.writeStartElement("VideoImagePath");
    xmlWriter.writeCharacters("");

    xmlWriter.writeEndElement();                // 结束根元素
    xmlWriter.writeEndDocument();               // 结束文档
    xmlFile.close();
    return true;
}

bool XMLFunc::ReadXmlData(const QString &filePath, QDomDocument *document)
{
    //document = nullptr;
    QFile xmlFile(filePath);
    if(!xmlFile.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        ELOG(QString("文件打开失败: " + filePath));
        xmlFile.close();
        return false;
    }

    QString erroeMsg;
    if(!document->setContent(&xmlFile, &erroeMsg))
    {
        ELOG(QString("不存在该xml文件" + erroeMsg));
        return false;
    }
    QDomElement root = document->documentElement();         // 获取根元素
    if(root.isNull())         // 如果root为空表示xml文件中为空，应该进行初始化
    {
        xmlFile.close();
        if(!ConfigXmlFileInit(filePath)) return false;
    }
    xmlFile.close();
    return true;
}

QString XMLFunc::ReadElementValue(const QDomDocument *document, const QString &key)
{
    QString keyValue;
    if(document == nullptr || key.isEmpty())
    {
        ELOG("传入参数异常");
        return keyValue;
    }

    QDomElement root = document->documentElement();
    // 查找并修改<Key>元素
    QDomNodeList Languages = root.elementsByTagName(key);
    if(Languages.isEmpty())
    {
       ELOG(QString("该xml文件不存在<" + key + "> 的元素"));
       return keyValue;
    }
    QDomElement LanguageElement = Languages.at(0).toElement();
    keyValue = LanguageElement.text();
    return keyValue;
}

bool XMLFunc::XmlDataModify(QDomDocument *document, const QString &Key, const QString &text)
{
    if(document == nullptr || Key.isEmpty() || text.isEmpty())
    {
        ELOG("传入参数异常");
        return false;
    }

    QDomElement root = document->documentElement();
    // 查找并修改<Key>元素
    QDomNodeList Languages = root.elementsByTagName(Key);
    if(Languages.isEmpty())
    {
       ELOG(QString("该xml文件不存在<" + Key + "> 的元素"));
       return false;
    }

    QDomElement LanguageElement = Languages.at(0).toElement();
    if(!LanguageElement.isNull())
    {
        LanguageElement.firstChild().setNodeValue(text);
    }

    return true;
}

bool XMLFunc::saveDocumentToXml(QDomDocument *document, const QString &filePath)
{
    if(document == nullptr || filePath.isEmpty())
    {
        ELOG("传入参数异常");
        return false;
    }
    QFile xmlFile(filePath);
    if(!xmlFile.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        ELOG("对应xml文件打开异常-需排查!(该文件/目录是否有权限操作)");
        return false;
    }
    QTextStream out (&xmlFile);
    document->save(out, 4);
    xmlFile.close();

    return true;
}

bool XMLFunc::xmlValueModify(const QString &filePath, const QString &Key, const QString &text)
{
    if(filePath.isEmpty() || Key.isEmpty() || text.isEmpty())
    {
        ELOG("传入参数异常");
        return false;
    }

    QFile xmlFile(filePath);
    if(!xmlFile.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        ELOG("文件打开异常");
        return false;
    }

    QDomDocument document;
    if(!document.setContent(&xmlFile))
    {
        ELOG(QString("不存在该xml文件: " + filePath));
        return false;
    }

    if(!XmlDataModify(&document, Key, text)) return false;
    xmlFile.close();
    return saveDocumentToXml(&document, filePath);
}

void XMLFunc::xmlPathInit()
{
    XMLFunc::ConfigXmlPath = QCoreApplication::applicationDirPath() + XMLPATH;
}

QString XMLFunc::xmlReadKeyValue(const QString &filePath, const QString &key)
{
    QDomDocument tmpDocument;
    if(ReadXmlData(filePath, &tmpDocument)){
        return ReadElementValue(&tmpDocument, key);
    }

    return QString();
}
